$(function () {
	

	var _$search = $('#search');
	//查询
	(function (_$search) {
		var _search = {
			init: function (opt) {
				var _it = this;
				_it.$error = opt.$error;
			},
			//校验输入值
			valiSearch: function (val) {
				if (!val) {
					return false;
				}
				var _isUrl = /^[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})*\.[a-zA-Z]{2,8}(?::\d{2,5})?$/.test(val) || /^(((https?|s?ftp):\/\/)?)(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(val);
				var _isPhone = /^(\d{5}|\d{3,4}(|-)\d{3,4}(|-)\d{3,4})$/.test(val);
				var _isOrg = /^[a-zA-Z\u4e00-\u9fa5\d()（）]+$/.test(val);
				return _isUrl || _isPhone || _isOrg;
			},
			//显示错误提示
			showError: function (msg) {
				this.$error.text(msg);
			}
		};
		_search.init({
			$error: $('#searchError')
		});

		_$search.attr('novalidate', 'novalidate').on('submit', function (e) {
			var form = this;
			var searchVal = form.search_key.value;
			var valiRet = _search.valiSearch(searchVal);
			if (!valiRet) {
				_search.showError("请输入正确的网站地址、电话号码或主办方名称");
				return false;
			}
		});
	})(_$search);

	// 匹配查询字符串中的query
	(function (_$search) {
		var result = window.location.search.match(/^\?query=(.+)$/);
		if (result && result.length == 2) {
			var query = result[1];
			// 存在query，则执行查询操作
			if (query) {
				_$search.find("[name='search_key']").val(query);
				_$search.submit();
			}
		}
	})(_$search);
});